package lp;

/**
 * @author lipeng
 * 盛最多水的容器
 */
public class LeetCode_11 {


    public static void main(String[] args) {
        int[] nums = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7};
        int i = maxArea2(nums);
        System.out.println(i);
    }

    /**
     * 1.暴力解法
     *
     * @param height
     * @return
     */
    public static int maxArea1(int[] height) {
        int maxarea = 0;
        for (int i = 0; i < height.length - 1; i++) {
            for (int j = i + 1; j < height.length; j++) {
                int min = Math.min(height[i], height[j]) * (j - i);
                maxarea = Math.max(maxarea, min);
            }
        }
        return maxarea;
    }

    /**
     * 2.double pointer
     *
     * @param height array
     * @return MaxArea
     */
    public static int maxArea2(int[] height) {
        int maxArea = 0;
        for (int i = 0, j = height.length - 1; i < j; ) {
            int minHeight;
            if (height[i] < height[j]) {
                minHeight = height[i];
                i++;
            } else {
                minHeight = height[j];
                j--;
            }
            int area = (j - i + 1) * minHeight;
            maxArea = Math.max(area, maxArea);
        }
        return maxArea;
    }
}
